using System.Web.Mvc;
using ilgolosone.DomainModel.Repositories;
using ilgolosone.Persistence.NH.Data;
using ilgolosone.Persistence.NH.Data.Impl;
using ilgolosone.Persistence.NH.Repositories;
using ilgolosone.Shared.Data;
using StructureMap.Attributes;
using StructureMap.Configuration.DSL;
using StructureMap.Graph;

namespace ilgolosone.DependencyResolver.SM
{
    public class DefaultRegistry : Registry
    {
        public DefaultRegistry()
        {
            ForRequestedType<IControllerFactory>()
                .TheDefaultIsConcreteType<StructureMapControllerFactory>();
           
            ForRequestedType<ISessionCreator>()
                .TheDefaultIsConcreteType<NHibernateStandardSessionCreator>()
                .AsSingletons();
            
            ForRequestedType<IUnitOfWork>()
                .TheDefault.Is.ConstructedBy( context => context.GetInstance<INHibernateUnitOfWork>() );
 
            ForRequestedType<INHibernateUnitOfWork>()
                .CacheBy(InstanceScope.Hybrid)
                .TheDefault.Is.OfConcreteType<NHibernateUnitOfWork>();

            ForRequestedType(typeof(IRepository<>))
                .TheDefaultIsConcreteType(typeof(NHibernateRepository<>));
            
            ForRequestedType(typeof(ISpecialtyRepository))
                .TheDefaultIsConcreteType(typeof(SpecialtyRepository));

            ForRequestedType(typeof(IRestaurantRepository))
                .TheDefaultIsConcreteType(typeof(RestaurantRepository));

            ForRequestedType<IReservationRepository>()
                .TheDefaultIsConcreteType<ReservationRepository>();

            Scan(x =>
                {
                    x.TheCallingAssembly();
                    x.With<DefaultConventionScanner>();
                });
        }
    }
}